/*
 * license-updater - Copyright (c) 2012 MSF. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */
package br.msf.netbeans.licenseupdater;

import br.msf.commons.netbeans.swing.model.JavaPackageComboBoxModel;
import br.msf.commons.netbeans.swing.model.ProjectComboBoxModel;
import br.msf.commons.netbeans.swing.renderer.JavaPackageListCellRenderer;
import br.msf.commons.netbeans.swing.renderer.ProjectListCellRenderer;
import br.msf.commons.netbeans.util.ClassPathUtils;
import br.msf.commons.netbeans.util.JavaPackage;
import br.msf.commons.netbeans.util.PackageUtils;
import br.msf.commons.netbeans.util.ProjectUtils;
import br.msf.commons.util.IOUtils;
import br.msf.netbeans.licenseupdater.LicenseUpdaterParams.UpdateMode;
import java.util.Collection;
import javax.swing.JPanel;
import org.netbeans.api.project.Project;
import org.openide.loaders.DataObject;

public final class LicenseUpdaterVisualPanel1 extends JPanel {

    private final LicenseUpdaterParams params;

    /** Creates new form LicenseUpdaterVisualPanel1 */
    public LicenseUpdaterVisualPanel1() {
        initComponents();
        this.params = new LicenseUpdaterParams();
        updateProjectCombo();
        updatePackageCombo();
        jTextField1.setText(params.getProjectPublisher());
        licenseTextArea.setText(params.getLicenseText());
    }

    @Override
    public String getName() {
        return "Step #1";
    }

    private void updateProjectCombo() {
        Collection<Project> projects = ProjectUtils.getOpenProjects();
        ((ProjectComboBoxModel) projectCombo.getModel()).addAll(projects);
        if (!projects.isEmpty()) {
            final Project selected = ProjectUtils.getSelectedProject();
            if (selected != null) {
                projectCombo.setSelectedItem(selected);
            } else {
                projectCombo.setSelectedIndex(0);
            }
        }
    }

    private void updatePackageCombo() {
        Project p = (Project) projectCombo.getSelectedItem();
        if (p == null) {
            throw new IllegalStateException("Project cannot be null.");
        }
        Collection<JavaPackage> packages = ProjectUtils.getSourcePackages(p, true);
        JavaPackageComboBoxModel model = new JavaPackageComboBoxModel();
        model.addAll(packages);
        packageCombo.setModel(model);
        if (model.getSize() > 0) {
            DataObject data = PackageUtils.getSelectedPackage();
            if (data != null) {
                try {
                    final JavaPackage selected = new JavaPackage(ClassPathUtils.getSourceClassPath(p), data.getPrimaryFile());
                    packageCombo.setSelectedItem(selected);
                } catch (Exception ex) {
                    packageCombo.setSelectedIndex(0);
                }
            } else {
                packageCombo.setSelectedIndex(0);
            }
        }
    }

    public LicenseUpdaterParams getLicenseUpdaterParams() {
        params.setProject((Project) projectCombo.getSelectedItem());
        params.setBasePackage((JavaPackage) packageCombo.getSelectedItem());
        params.setProjectPublisher(jTextField1.getText());
        params.setUpdateMode(withoutLicenseRadio.isSelected() ? UpdateMode.WITHOUT_LICENSE_FILES : UpdateMode.ALL_FILES);
        params.setLicenseText(licenseTextArea.getText());
        return params;
    }

    private void openTemplate(String key) {
        String s = IOUtils.asText(getClass().getResourceAsStream("/br/msf/netbeans/licenseupdater/licenses/" + key), "UTF-8");
        if (s != null) {
            licenseTextArea.setText(s);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        licenseMenu = new javax.swing.JPopupMenu();
        lgpl2 = new javax.swing.JMenuItem();
        lgpl3 = new javax.swing.JMenuItem();
        gpl2 = new javax.swing.JMenuItem();
        gpl3 = new javax.swing.JMenuItem();
        apache2 = new javax.swing.JMenuItem();
        cddl1 = new javax.swing.JMenuItem();
        projectCombo = new javax.swing.JComboBox();
        packageCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        allFilesRadio = new javax.swing.JRadioButton();
        withoutLicenseRadio = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        licenseTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        clearBtn = new javax.swing.JButton();
        licenseSelectorBtn = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(lgpl2, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.lgpl2.text")); // NOI18N
        lgpl2.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.lgpl2.toolTipText")); // NOI18N
        lgpl2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lgpl2ActionPerformed(evt);
            }
        });
        licenseMenu.add(lgpl2);

        org.openide.awt.Mnemonics.setLocalizedText(lgpl3, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.lgpl3.text")); // NOI18N
        lgpl3.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.lgpl3.toolTipText")); // NOI18N
        lgpl3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lgpl3ActionPerformed(evt);
            }
        });
        licenseMenu.add(lgpl3);

        org.openide.awt.Mnemonics.setLocalizedText(gpl2, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.gpl2.text")); // NOI18N
        gpl2.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.gpl2.toolTipText")); // NOI18N
        gpl2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gpl2ActionPerformed(evt);
            }
        });
        licenseMenu.add(gpl2);

        org.openide.awt.Mnemonics.setLocalizedText(gpl3, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.gpl3.text")); // NOI18N
        gpl3.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.gpl3.toolTipText")); // NOI18N
        gpl3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gpl3ActionPerformed(evt);
            }
        });
        licenseMenu.add(gpl3);

        org.openide.awt.Mnemonics.setLocalizedText(apache2, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.apache2.text")); // NOI18N
        apache2.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.apache2.toolTipText")); // NOI18N
        apache2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apache2ActionPerformed(evt);
            }
        });
        licenseMenu.add(apache2);

        org.openide.awt.Mnemonics.setLocalizedText(cddl1, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.cddl1.text")); // NOI18N
        cddl1.setToolTipText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.cddl1.toolTipText")); // NOI18N
        cddl1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cddl1ActionPerformed(evt);
            }
        });
        licenseMenu.add(cddl1);

        projectCombo.setModel(new ProjectComboBoxModel());
        projectCombo.setRenderer(new ProjectListCellRenderer());
        projectCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                projectComboActionPerformed(evt);
            }
        });

        packageCombo.setRenderer(new JavaPackageListCellRenderer());

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.jLabel2.text")); // NOI18N

        buttonGroup1.add(allFilesRadio);
        org.openide.awt.Mnemonics.setLocalizedText(allFilesRadio, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.allFilesRadio.text")); // NOI18N

        buttonGroup1.add(withoutLicenseRadio);
        withoutLicenseRadio.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(withoutLicenseRadio, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.withoutLicenseRadio.text")); // NOI18N

        licenseTextArea.setColumns(20);
        licenseTextArea.setRows(5);
        licenseTextArea.setText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.licenseTextArea.text")); // NOI18N
        licenseTextArea.setLineWrap(true);
        jScrollPane1.setViewportView(licenseTextArea);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.jLabel3.text")); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        clearBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/msf/netbeans/licenseupdater/clear.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(clearBtn, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.clearBtn.text")); // NOI18N
        clearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(clearBtn);

        licenseSelectorBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/msf/netbeans/licenseupdater/license.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(licenseSelectorBtn, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.licenseSelectorBtn.text")); // NOI18N
        licenseSelectorBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                licenseSelectorBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(licenseSelectorBtn);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.jLabel4.text")); // NOI18N

        jTextField1.setText(org.openide.util.NbBundle.getMessage(LicenseUpdaterVisualPanel1.class, "LicenseUpdaterVisualPanel1.jTextField1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(packageCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(projectCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(withoutLicenseRadio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(allFilesRadio))
                            .addComponent(jLabel3))
                        .addGap(0, 128, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(packageCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(allFilesRadio)
                    .addComponent(withoutLicenseRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void projectComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_projectComboActionPerformed
        updatePackageCombo();
    }//GEN-LAST:event_projectComboActionPerformed

    private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearBtnActionPerformed
        // TODO add your handling code here:
        licenseTextArea.setText("");
    }//GEN-LAST:event_clearBtnActionPerformed

    private void licenseSelectorBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_licenseSelectorBtnActionPerformed
        // TODO add your handling code here:
        if (licenseMenu.isShowing()) {
            licenseMenu.setVisible(false);
        } else {
            licenseMenu.show(licenseSelectorBtn, 0, licenseSelectorBtn.getHeight());
        }

    }//GEN-LAST:event_licenseSelectorBtnActionPerformed

    private void gpl3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gpl3ActionPerformed
        openTemplate(GPL3_REF);
    }//GEN-LAST:event_gpl3ActionPerformed

    private void lgpl2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lgpl2ActionPerformed
        openTemplate(LGPL21_REF);
    }//GEN-LAST:event_lgpl2ActionPerformed

    private void lgpl3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lgpl3ActionPerformed
        openTemplate(LGPL3_REF);
    }//GEN-LAST:event_lgpl3ActionPerformed

    private void gpl2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gpl2ActionPerformed
        openTemplate(GPL2_REF);
    }//GEN-LAST:event_gpl2ActionPerformed

    private void apache2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apache2ActionPerformed
        openTemplate(APACHE2_REF);
    }//GEN-LAST:event_apache2ActionPerformed

    private void cddl1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cddl1ActionPerformed
        openTemplate(CDDL1_REF);
    }//GEN-LAST:event_cddl1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton allFilesRadio;
    private javax.swing.JMenuItem apache2;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JMenuItem cddl1;
    private javax.swing.JButton clearBtn;
    private javax.swing.JMenuItem gpl2;
    private javax.swing.JMenuItem gpl3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem lgpl2;
    private javax.swing.JMenuItem lgpl3;
    private javax.swing.JPopupMenu licenseMenu;
    private javax.swing.JButton licenseSelectorBtn;
    private javax.swing.JTextArea licenseTextArea;
    private javax.swing.JComboBox packageCombo;
    private javax.swing.JComboBox projectCombo;
    private javax.swing.JRadioButton withoutLicenseRadio;
    // End of variables declaration//GEN-END:variables
    private static final String CDDL1_REF = "cddl1_ref";
    private static final String APACHE2_REF = "apache2_ref";
    private static final String GPL2_REF = "gpl2_ref";
    private static final String GPL3_REF = "gpl3_ref";
    private static final String LGPL21_REF = "lgpl21_ref";
    private static final String LGPL3_REF = "lgpl3_ref";
}
